/*
 * 本类作用：测试
 * 
 * 作者：袁小杰
 * 博客：http://blog.csdn.net/undoner
 * GIT：https://git.oschina.net/undoner
 * QQ：15137281
 * 
 */
package org.mina.test;

import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;


public class SessionFactory {
	static Logger log = Logger.getRootLogger();
	private static  SessionFactory instance;
	private  SqlSessionFactory sqlSessionFactory;
	private  Reader reader;
	
	public  synchronized static SessionFactory getInstance(){
	    if (instance == null) {
	      instance =new SessionFactory();
	    }
	    return instance;
	  }

	public SessionFactory() {
		try {
			String filePath= System.getProperty("user.dir")+ File.separator + "config"
					+ File.separator +"\\MybatisConfig.xml";
			//绝对路径获取文件：
			reader = new FileReader(new File(filePath));
			//如果是src目录下，可使用以下路径：
			//reader = Resources.getResourceAsReader(filePath);
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
	}
	
	public SqlSessionFactory getSession() {
		return sqlSessionFactory;
	}

}
